package com.example.multity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import java.util.Locale

class FifthActivity : ComponentActivity() {
    var seconds=0
    var running=false
    var wasrunning=false
    private lateinit var textview_timer: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fifth)

        val buttonstart=findViewById<Button>(R.id.button_start)
        val buttonstop=findViewById<Button>(R.id.button_stop)
        val buttonrestart=findViewById<Button>(R.id.button_restart)
        val buttonback=findViewById<Button>(R.id.button_fifth_back)
        textview_timer=findViewById(R.id.textView_timer)
        Log.d( "Life cycle","onCreate")
        if(savedInstanceState!=null)
        {
            seconds=savedInstanceState.getInt("seconds")
            running=savedInstanceState.getBoolean("running")
            wasrunning=savedInstanceState.getBoolean("wasrunning")
        }
        runTimer()
        buttonstart.setOnClickListener {
            running=true
        }
        buttonstop.setOnClickListener {
            running=false
        }
        buttonrestart.setOnClickListener {
            running=true
            seconds=0
        }
        buttonback.setOnClickListener {
            finish()
        }

    }
    fun runTimer() {
        val handler = android.os.Handler(Looper.getMainLooper())

        val runnable = object : Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60
                // 格式化为 HH:MM:SS
                textview_timer.text = String.format(Locale.US,"%02d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000)// 每隔 1000ms (1秒) 再次调用自己
            }
        }
        handler.post(runnable)// 启动定时器：第一次调用 runnable
    }
    override fun onSaveInstanceState(outState: Bundle){
        super.onSaveInstanceState(outState)
        Log.d( "Life cycle","onSaveInstanceState")
        outState.putInt("seconds",seconds)
        outState.putBoolean("running",running)
        outState.putBoolean("wasRunning",wasrunning)
    }
    override fun onDestroy() {
        super.onDestroy()
        Log.d("Life cycle", "onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("Life cycle", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("Life cycle", "onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("Life cycle", "onResume")
        if(wasrunning){
            running = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("Life cycle", "onPause")
        wasrunning = running
        running = false
    }
}